#include <bits/stdc++.h>

using namespace std;

const int N = 510;

int f[N][N];
class Solution
{
public:
    int minDistance(string word1, string word2)
    {
        memset(f, sizeof(f), 0);

        for (int i = 0; i <= word1.size(); ++i)
        {
            f[i][0] = i;
        }
        for (int j = 0; j <= word2.size(); ++j)
        {
            f[0][j] = j;
        }
        for (int i = 1; i <= word1.size(); ++i)
        {
            for (int j = 1; j <= word2.size(); ++j)
            {
                f[i][j] = min(f[i - 1][j], f[i][j - 1]) + 1;
                f[i][j] = min(f[i][j], f[i - 1][j - 1] + (word1[i - 1] != word2[j - 1]));
            }
        }
        return f[word1.size()][word2.size()];
    }
};